/**
 * Vosao CMS. Simple CMS for Google App Engine.
 * Copyright (C) 2009 Vosao development team
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 * email: vosao.dev@gmail.com
 */

package org.vosao.plugins.csdata.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.vosao.plugins.csdata.dao.CsdataDao;
import org.vosao.plugins.csdata.entity.CsdataUserEntity;

import org.vosao.plugins.csdata.service.CsdataFrontService;

//import com.google.appengine.api.users.UserService;
//import com.google.appengine.api.users.UserServiceFactory;

import com.google.appengine.api.oauth.InvalidOAuthParametersException;
import com.google.appengine.api.oauth.InvalidOAuthTokenException;
import com.google.appengine.api.oauth.OAuthRequestException;
import com.google.appengine.api.oauth.OAuthService;
import com.google.appengine.api.oauth.OAuthServiceFactory;
import com.google.appengine.api.oauth.OAuthServiceFailureException;
import com.google.appengine.api.users.User;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;


//import java.io.IOException;
//import java.util.Map;
//import java.util.Map.Entry;
//import java.util.logging.Level;
//import java.util.logging.Logger;




public class CsdataServlet extends HttpServlet {

	private CsdataDao csdataDao;
	//private CsdataBackService csdataBackService;
	private CsdataFrontService csdataFrontService;
	
	public CsdataServlet(CsdataDao aCsdataDao,
			CsdataFrontService aCsdataFrontService) {
		setCsdataDao(aCsdataDao);
		setCsdataFrontService(aCsdataFrontService);
	}
	
	public void doGet(HttpServletRequest req, HttpServletResponse res)
			throws ServletException, IOException {
		//String sessionKey = request.getParameter("id");
		
		//UserService userService = UserServiceFactory.getUserService();
		JsonObject output = new JsonObject();
		try {
			OAuthService oauthService = OAuthServiceFactory.getOAuthService();
		      
			User user = oauthService.getCurrentUser();
		
			CsdataUserEntity csdataUserEntity = csdataDao.getCsdataUserDao().getByEmail(user.getEmail());
			
			output.addProperty("status", "ok");
			output.addProperty("nickname", user.getNickname());
		    output.addProperty("email", user.getEmail());
		    output.addProperty("user_id", user.getUserId());
		    output.addProperty("is_admin", oauthService.isUserAdmin());
		    
						
			if (csdataUserEntity == null){
				
				//res.getWriter().println(user.getEmail());
				output.addProperty("_data","");
			    
			}else
			{ output.addProperty("_data",csdataUserEntity.getData());
		    }
		
		
		//CsdataUserEntity reg = getCsdataDao().getCsdataUserDao().getByEmail();
		//		.getBySessionKey(sessionKey);
		
		//CsdataUserEntity reg = getCsdataDao().getCsdataUserDao().getByEmail(email)
		//		.getBySessionKey(sessionKey);
		//if (reg != null) {
		//	getCsdataBackService().confirmRegistration(reg.getId());
		//}
		
		//response.sendRedirect("/");
			res.setStatus(HttpServletResponse.SC_OK);
		} catch (InvalidOAuthTokenException e) {
			      res.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
			      output.addProperty("status", "invalid token");
			      //logger.log(Level.INFO, "request validation failure", e);
			    } catch (InvalidOAuthParametersException e) {
			      res.setStatus(HttpServletResponse.SC_BAD_REQUEST);
			      output.addProperty("status", "invalid parameters");
			      //logger.log(Level.INFO, "request contained invalid token", e);
			    } catch (OAuthRequestException e) {
			      res.setStatus(HttpServletResponse.SC_BAD_REQUEST);
			      output.addProperty("status", "bad request");
			      //logger.log(Level.INFO, "request validation failure", e);
			    } catch (OAuthServiceFailureException e) {
			      res.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
			      output.addProperty("status", "unexpected error");
			      //logger.log(Level.INFO, "request validation failure", e);
			    }
			
		
		res.setHeader("Content-Type", "application/json");
		Gson gson = new GsonBuilder().setPrettyPrinting().create();
		res.getWriter().write(gson.toJson(output));
}

	public CsdataDao getCsdataDao() {
		return csdataDao;
	}

	public void setCsdataDao(CsdataDao csdataDao) {
		this.csdataDao = csdataDao;
	}

	public CsdataFrontService getCsdataFrontService() {
		return csdataFrontService;
	}

	public void setCsdataFrontService(CsdataFrontService csdataFrontService) {
		this.csdataFrontService = csdataFrontService;
	}
	
}
